package view 
{
	import com.bit101.components.CheckBox;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import com.bit101.components.Style;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	import model.vos.movieType;
	
	/**
	 * 参数面板
	 * @author	Meathill
	 * @version	0.1(2011-03-20)
	 */
	public class configPanelView extends Sprite
	{
		public static const PREVIEW:String = 'preview';
		public static const SAVE:String = 'save';
		
		public var type:int = 0;
		
		private var _width_txt:InputText;
		private var _height_txt:InputText;
		private var _rows_txt:InputText;
		private var _cols_txt:InputText;
		private var _is_limited:CheckBox;
		private var _file:FileReference;
		private var _file_filters:Array;
		
		public function configPanelView(parent:DisplayObjectContainer = null, posx:int = 0, posy:int = 0) 
		{
			x = posx, y = posy;
			if (parent != null) parent.addChild(this);
			init();
		}
		
		/************
		 * properties
		 * *********/
		public function get data():ByteArray {
			return _file.data;
		}
		override public function get width():Number { 
			return int(_width_txt.text); 
		}
		override public function set width(value:Number):void {
			_width_txt.text = value.toString();
		}
		override public function get height():Number {
			return int(_height_txt.text);
		}
		override public function set height(value:Number):void {
			_height_txt.text = value.toString();
		}
		public function set rows(num:int):void {
			_rows_txt.text = num.toString();
		}
		public function get rows():int {
			return int(_rows_txt.text);
		}
		public function set cols(num:int):void {
			_cols_txt.text = num.toString();
		}
		public function get cols():int {
			return int(_cols_txt.text);
		}
		public function get isLimited():Boolean {
			return _is_limited.selected;
		}
		
		/************
		 * functions
		 * *********/
		private function init():void {
			Style.BACKGROUND = 0xf0f0f0;
			
			var _label:Label = new Label(this, 0, -2, '宽：');
			_width_txt = new InputText(this, 30, 0, '100');
			_width_txt.setSize(40, 18);
			
			_label = new Label(this, 90, -2, '高：');
			_height_txt = new InputText(this, 120, 0, '100');
			_height_txt.setSize(40, 18);
			
			_label = new Label(this, 0, 28, '行：');
			_rows_txt = new InputText(this, 30, 30, '4');
			_rows_txt.setSize(40, 18);
			
			_label = new Label(this, 90, 28, '列：');
			_cols_txt = new InputText(this, 120, 30, '4');
			_cols_txt.setSize(40, 18);
			_width_txt.restrict = _height_txt.restrict = _rows_txt.restrict = _cols_txt.restrict = '0-9';
			
			_is_limited = new CheckBox(this, 180, 2, '限制输出数量');
			
			_file = new FileReference();
			_file.addEventListener(Event.SELECT, onSelect);
			_file.addEventListener(Event.COMPLETE, onLoadComplete);
			_file_filters = [new FileFilter('GIF图片', '*.gif'), new FileFilter('Flash动画', '*.swf')];
			
			var _browse_btn:PushButton = new PushButton(this, 0, 60, '选择文件', onBrowseBtnClick);
			var _preview_btn:PushButton = new PushButton(this, 105, 60, '预览', onPreview);
			var _save_btn:PushButton = new PushButton(this, 210, 60, '保存', onSave);
		}
		private function onBrowseBtnClick(evt:MouseEvent):void {
			_file.browse(_file_filters);
		}
		private function onSelect(evt:Event):void {
			trace(_file.name);
			type = _file.name.indexOf('.swf') != -1 ? movieType.SWF : movieType.GIF;
			_file.load();
		}
		private function onLoadComplete(evt:Event):void {
			dispatchEvent(evt);
		}
		private function onPreview(evt:MouseEvent):void {
			dispatchEvent(new Event(PREVIEW));
		}
		private function onSave(evt:MouseEvent):void {
			dispatchEvent(new Event(SAVE));
		}
	}
}